/**
 * Created by zhgk on 2016/9/21 0021.
 */
import {Component, OnInit, Input} from "@angular/core";
import {DtConfig} from "./dt.mode";
import {isUndefined, isNullOrUndefined} from "util";
import {HttpService} from "../httpService";
import {ToasterService} from "angular2-toaster";
@Component({
    selector: 'dt_table',
    template: require("./dt.component.html"),
    styles: [require("./dt.component.css")]
})

export class DataTableComponent implements OnInit {
    //变量定义
    @Input()
    dtConfig: DtConfig;

    //总页数
    private totalPage: number = 1;

    //当前选中页
    private currentPage: number = 1;
    //
    private totalRows: number = 0;

    //分页标签开始显示页
    private startPage: number = 1;

    //当前要展示的数据
    private showData: any[] = [];

    //当前记录
    private selectRow: any = {};

    constructor(private ts: ToasterService, private hs: HttpService) {

    }

    //初始化方法
    ngOnInit(): void {

        //初始化数据
        if (isNullOrUndefined(this.dtConfig.data)) {
            this.dtConfig.data = [];
        }

        this.totalRows = this.dtConfig.data.length;

        //初始化每页记录选项
        if (isNullOrUndefined(this.dtConfig.pageSizeList)) {
            this.dtConfig.pageSizeList = [10, 50, 100, 200];
        }

        //初始化每页记录数大小
        if (isNullOrUndefined(this.dtConfig.pageSize)) {
            this.dtConfig.pageSize = this.dtConfig.pageSizeList[0];
        }

        //将自定义页数加入页数可选列表
        if (this.dtConfig.pageSizeList.indexOf(this.dtConfig.pageSize) < 0) {
            this.dtConfig.pageSizeList.push(+this.dtConfig.pageSize);
            this.dtConfig.pageSizeList.sort((n1, n2) => {
                if (+n1 > +n2) {
                    return 1;
                }
                if (+n1 < +n2) {
                    return -1;
                }
                return 0;
            });
        }

        //初始化总页数
        this.computePage();

        //默认不分页
        if (isNullOrUndefined(this.dtConfig.paging)) {
            this.dtConfig.paging = false;
        }

        //是否远程加载
        if (isNullOrUndefined(this.dtConfig.remote)) {
            this.dtConfig.remote = false;
        }
        //加载数据
        this.loadData();

        //宽度
        if (isNullOrUndefined(this.dtConfig.width)) {
            this.dtConfig.width = "100%";
        }
        if (isNullOrUndefined(this.dtConfig.height)) {
            this.dtConfig.height = "100%";
        }

    }

    //更新当前选中页
    private changCurrentPageNext(next: boolean): void {
        if (next) {
            if (this.currentPage < this.totalPage) {
                this.currentPage += 1;
                //向后变更开始页
                if (this.currentPage >= this.startPage + 5 && this.currentPage > 5) {
                    this.startPage += 5;
                }
            }

        } else {
            //向前选中
            if (this.currentPage > 1) {
                this.currentPage -= 1;
                //向前变更开始页
                if (this.currentPage < this.startPage) {
                    this.startPage -= 5;
                }
            }

        }

        this.loadData();
    }

    //选择当前页
    private selectCurrentPage(page: number): void {
        this.currentPage = page;
        this.loadData();
    }

    //前往首页
    private goFirstPage(): void {
        this.currentPage = 1;
        this.startPage = 1;
        this.loadData();
    }

    //前往末页
    private goEndPage(): void {
        this.currentPage = this.totalPage;
        if (this.totalPage > 5) {
            this.startPage = parseInt((this.totalPage / 5) + "") * 5 + 1;
        } else {
            this.startPage = 1;
        }
        this.loadData();
    }


    private  loadData(): void {
        if (this.dtConfig.remote) {
            this.showRemoteData();
        } else {
            this.showLocalData();
        }
    }

    //展示本地数据
    private showLocalData(): void {
        //this.computePage();
        this.showData = this.dtConfig.data.slice((this.currentPage - 1) * this.dtConfig.pageSize, this.currentPage * this.dtConfig.pageSize);

    }

    //加载远程数据
    private showRemoteData(): void {
        if (isNullOrUndefined(this.dtConfig.url) || this.dtConfig.url == "") {
            this.ts.pop("error", "系统消息", "表格Remote属性为真时，URL不能为空！");
            return;
        }
        let url = this.dtConfig.url;
        //增加参数：时间
        if (url.indexOf("?") >= 0) {
            url += "&sid=" + new Date().getTime();
        } else {
            url += "?sid=" + new Date().getTime();
        }
        if (this.dtConfig.paging) {
            //增加当前页、页面大小
            url += "&start=" + this.currentPage + "?limit=" + this.dtConfig.pageSize;
        }
        //加载远程数据
        this.hs.query(url).subscribe(res=> {
            this.showData = res.data;
            this.totalRows = isNullOrUndefined(res.total) ? res.data.length : res.total;
            this.computePage();
        })

    }

    //变更业面记录大小
    private changPageSize(ev: any): void {
        this.dtConfig.pageSize = ev.target.value;
        this.computePage();
        this.loadData();
    }

    //从新计算分页与分页大小
    private computePage(): void {
        //初始化总页数
        this.totalPage = parseInt((this.totalRows / this.dtConfig.pageSize) + '');
        if (this.totalRows % this.dtConfig.pageSize > 0) {
            this.totalPage += 1;
        }
        //从新设定开启页和当前页
        this.startPage = this.currentPage = 1;
    }

    //选择当前记录
    private  selectRecord(row: any): void {
        this.selectRow = row;
    }

    //对外接口
    //刷新数据
    refreshData(): void {
        this.loadData();
    }

    getSelectedRow(): any {
        if (this.selectRow) {
            return this.selectRow;
        } else {
            return null;
        }
    }
}